﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data;
using MySql.Data;
using System.Data.SQLite;

namespace DB
{
    public class SQLiteUtils
    {
                      
        SQLiteConnection m_dbConnection=new SQLiteConnection("Data Source=C:\\sqlite\\demo.db");

        ~SQLiteUtils()
        {
            try
            {
                
                m_dbConnection.Close();
                m_dbConnection.Dispose();
            }
            catch
            {
            }
            finally
            {
            }

        }

        void connectDabaBase()
        {
           
        }

        public Object InsertOrUpdateData(String sql)
        {
            try
            {
                m_dbConnection.Open();

                SQLiteCommand commomd = new SQLiteCommand(sql, m_dbConnection);
                Object result = commomd.ExecuteScalar();// ExecuteReader();// ExecuteNonQuery();
                return result;
            }
            finally
            {
                m_dbConnection.Close();

            }
        }

        public Object fetchResult(String sql)
        {
            try
            {
                m_dbConnection.Open();

                SQLiteCommand commomd = new SQLiteCommand(sql, m_dbConnection);
                Object result = commomd.ExecuteScalar();// ExecuteReader();// ExecuteNonQuery();
                return result;
            }
            finally
            {
                m_dbConnection.Close();
            }
        }

        public DataTable fetchData(String sql)
        {
            try
            {
                SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(sql, m_dbConnection);
                DataTable dataTable = new DataTable();
                dataAdapter.Fill(dataTable);
                return dataTable;
            }
            finally
            {
                m_dbConnection.Close();
            }
        }

        public void DeleteData(String sql)
        {
            try
            {
                m_dbConnection.Open();
                SQLiteCommand commomd = new SQLiteCommand(sql, m_dbConnection);
                commomd.ExecuteNonQuery();// ExecuteReader();// ExecuteNonQuery();
            }
            finally
            {
                m_dbConnection.Close();
            }
        }

        public void DeleteData(String table, int id)
        {
            try
            {
                m_dbConnection.Open();
                String sql = "delete from " + table + " where id =" + id;

                SQLiteCommand commomd = new SQLiteCommand(sql, m_dbConnection);
                 commomd.ExecuteNonQuery();// ExecuteReader();// ExecuteNonQuery();
            }
            finally
            {
                m_dbConnection.Close();
            }
        }


    }
}
